<template>
  <el-dialog
    title="问题详情"
    :visible.sync="dialogVisible"
    center
    width="1200px"
    :showClose="false"
    :close-on-click-modal="false"
  >
    <div style="height:50vh;overflow: auto;">
      <el-form
        label-width="140px"
        :model="dialogData"
        style="width:90%"
        :rules="rules"
        ref="dialogData"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="检查人员：">
              <el-input v-model="dialogData.checkStaffName" style="width:100%" readonly></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="检查日期：">
              <el-date-picker
                v-model="dialogData.checkDate"
                style="width: 100%;"
                type="date"
                value-format="yyyy-MM-dd"
              ></el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="受检单位：">
              <el-cascader
                style="width:100%"
                v-model="checkCompanyList"
                :options="checkCompanyNameOptions"
                clearable
                filterable
                ref="refHandle"
                @change="checkCompanyListChange"
                :props="{ expandTrigger: 'hover', checkStrictly: true , value:'nodeCode', label:'label', children:'children'}"
                :show-all-levels="true"
              ></el-cascader>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="检查地点：">
              <el-input v-model="dialogData.address" style="width:100%"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-form-item label="检查类型：">
            <el-select style="width:200px" 
                v-model="dialogData.type" 
                placeholder="选择类型" 
                clearable>
                <el-option 
                v-for="item in types" 
                :key="item.id" 
                :label="item.name" 
                :value="item.name"
                ></el-option>
              </el-select>
          </el-form-item>
        </el-row>
        <el-form-item label="问题描述：">
          <el-input
            v-model="dialogData.problemDescription"
            style="width:100%"
            type="textarea"
            :autosize="{ minRows: 4}"
          ></el-input>
        </el-form-item>
        <el-form-item label="备注：">
          <el-input
            v-model="dialogData.note"
            style="width:100%"
            type="textarea"
            :autosize="{ minRows: 2}"
          ></el-input>
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="问题类型：">
              <el-input v-model="dialogData.problemType" style="width:100%"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="问题程度：">
              <el-select v-model="dialogData.problemRank" style="width:100%">
                <el-option
                  v-for="item in problemRankOptions"
                  :key="item.id"
                  :label="item.name"
                  :value="item.name"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-form-item label="问题体系要素：">
            <el-input v-model="dialogData.hseFactor"></el-input>
          </el-form-item>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="问题原因：">
              <el-input v-model="dialogData.problemSource" style="width:100%"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="安全绩效扣分：">
              <el-select v-model="dialogData.lostScore" style="width:100%">
                <el-option
                  v-for="item in scoreOptions"
                  :key="item.id"
                  :label="item.name"
                  :value="item.name"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="依据标准：">
          <el-input
            v-model="dialogData.problemGist"
            style="width:100%"
            type="textarea"
            :autosize="{ minRows: 1}"
          ></el-input>
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="责任人：" prop="responsePersonID">
              <el-select
                v-model="dialogData.responsePersonID"
                placeholder="输入姓名搜索"
                style="width:100%"
                filterable
                clearable
                @change="changeResponsibilityPerson"
              >
                <el-option
                  v-for="item in responsePersonOptions"
                  :key="item.id"
                  :label="`${item.name}`"
                  :value="item.employeeID"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用工性质：">
              <el-input v-model="dialogData.employmentProperty" style="width:100%"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="岗位工龄：">
              <el-select v-model="dialogData.workingAge" style="width:100%">
                <el-option
                  v-for="item in workingAgeOptions"
                  :key="item.id"
                  :label="item.name"
                  :value="item.name"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="岗位名称：">
              <el-select 
              style="width:100%" 
              v-model="dialogData.workingName" 
              placeholder="选择岗位" 
              clearable>
              <el-option 
              v-for="item in postNames" 
              :key="item.id" 
              :label="item.name" 
              :value="item.name">
              </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="整改责任人：" prop="supervisorID">
              <el-select
                v-model="dialogData.supervisorID"
                placeholder="输入姓名搜索"
                style="width:100%"
                @change="supervisorChanged"
                filterable
                clearable
              >
                <el-option
                  v-for="item in supervisorOptions"
                  :key="item.id"
                  :label="`${item.name}`"
                  :value="item.employeeID"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item :label="checkAttachLabel" style="white-space: pre-line;">
          <el-upload
            style="margin:20px"
            :action="`/api/file_upload`"
            list-type="picture-card"
            :limit="4"
            :before-upload="beforeAttachUpload"
            :on-remove="handleRemoveCheckAttach"
            :auto-upload="true"
            accept=".png, .jpg, .bmp, .tif, .gif, .jpeg"
            :headers="{Authorization:currentUser.token}"
            :on-success="handleSuccessCheckAttach"
            :file-list="checkAttachFileList"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="问题视频：">
          <el-row>
            <el-col :span="8">
              <el-upload
                v-model="dialogData.checkVideo"
                :action="`/api/file_upload`"
                :auto-upload="true"
                :limit="1"
                accept=".mp4, .qlv, .qsv, .ogg, .flv, .avi, .wmv, .rmvb"
                :headers="{Authorization:currentUser.token}"
                :before-upload="beforeAttachUpload"
                :on-remove="handleRemoveCheckVideo"
                :on-success="handleSuccessCheckVideo"
                list-type="text"
              >
                <el-button size="small" type="primary">点击上传</el-button>
                <div slot="tip" style="padding-right:20px">只能上传mp4、avi、flash、rmvb、rm格式，且不大于12M的视频。</div>
              </el-upload>
            </el-col>
            <el-col :span="16">
              <el-card
                :body-style="{ padding: '10px' }"
                style="width:100%;height:200px;text-align:center"
              >
                <span v-if="!dialogData.checkVideo && !checkVideourl">无视频文件记录！</span>
                <video
                  v-else-if="checkVideourl"
                  width="100%"
                  height="180"
                  controls
                  preload="none"
                  :src="checkVideourl"
                ></video>
                <video
                  v-else
                  width="100%"
                  height="180"
                  controls
                  preload="none"
                  :src="dialogData.checkVideo"
                ></video>
              </el-card>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item label="是否立即整改：">
          <el-checkbox
            v-model="immediateRectification"
            @change="changeImmediateRectification"
            :disabled="isImmediateRectification"
          >立即整改（默认为否，打√为是）</el-checkbox>
        </el-form-item>
        <!-- 是否立即整改 -->
        <div v-if="immediateRectification === true" style="border-top: 1px solid #dddddd;">
          <el-form-item label="整改情况如下："></el-form-item>
          <el-form-item label="是否批评教育：">
            <el-checkbox
              v-model="criticalEducation"
              @change="changeCriticalEducation"
            >批评教育（打√后才能保存问题）</el-checkbox>
          </el-form-item>
          <el-form-item label="整改描述：">
            <el-input
              v-model="dialogData.rectDescription"
              style="width:100%"
              type="textarea"
              :autosize="{ minRows: 4}"
            ></el-input>
          </el-form-item>
          <el-form-item :label="rectAttachLabel" style="white-space: pre-line;">
            <el-upload
              style="margin:20px"
              :action="`/api/file_upload`"
              list-type="picture-card"
              :limit="4"
              accept=".png, .jpg, .bmp, .tif, .gif, .jpeg"
              :before-upload="beforeAttachUpload"
              :on-remove="handleRemoveRectAttach"
              :auto-upload="true"
              :headers="{Authorization:currentUser.token}"
              :on-success="handleSuccessRectAttach"
              :file-list="rectAttachFileList"
            >
              <i class="el-icon-plus"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="整改视频：">
            <el-row>
              <el-col :span="8">
                <el-upload
                  v-model="dialogData.rectVideo"
                  :action="`/api/file_upload`"
                  :auto-upload="true"
                  :limit="1"
                  accept=".mp4, .qlv, .qsv, .ogg, .flv, .avi, .wmv, .rmvb"
                  :headers="{Authorization:currentUser.token}"
                  :before-upload="beforeAttachUpload"
                  :on-remove="handleRemoveRectVideo"
                  :on-success="handleSuccessRectVideo"
                  list-type="text"
                >
                  <el-button size="small" type="primary">点击上传</el-button>
                  <div slot="tip" style="padding-right:20px">只能上传mp4、avi、flash、rmvb、rm格式，且不大于12M的视频。</div>
                </el-upload>
              </el-col>
              <el-col :span="16">
                <el-card
                  :body-style="{ padding: '10px' }"
                  style="width:90%;height:200px;text-align:center"
                >
                  <span v-if="!dialogData.rectVideo && !rectVideourl">无视频文件记录！</span>
                  <video
                    v-else-if="rectVideourl"
                    width="100%"
                    height="180"
                    controls
                    preload="none"
                    :src="rectVideourl"
                  ></video>
                  <video
                    v-else
                    width="100%"
                    height="180"
                    controls
                    preload="none"
                    :src="dialogData.rectVideo"
                  ></video>
                </el-card>
              </el-col>
            </el-row>
          </el-form-item>
          <el-form-item label="整改附件：">
            <el-upload
              :action="`/api/file_upload`"
              :auto-upload="true"
              :limit="1"
              accept=".pdf, .txt, .doc, .docx, .xlsx, .pptx"
              :headers="{Authorization:currentUser.token}"
              :before-upload="beforeAttachUpload"
              :on-remove="handleRemoveRectFile"
              :on-success="handleSuccessRectFile"
              list-type="text"
              :file-list="rectFileList"
            >
              <el-button size="small" type="primary">点击上传</el-button>
            </el-upload>
          </el-form-item>
        </div>
        <div v-if="immediateRectification === true" style="border-top: 1px solid #dddddd;">
          <el-form-item label="验证情况如下："></el-form-item>
          <el-row>
            <el-col :span="12">
              <el-form-item label="验证人：">
                <el-input v-model="dialogData.verifyPersonName" style="width:100%" readonly></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="验证日期：">
                <el-date-picker
                  v-model="dialogData.verifyDate"
                  style="width: 100%;"
                  type="date"
                  value-format="yyyy-MM-dd"
                ></el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="整改结果：">
                <el-select v-model="dialogData.rectEffect" style="width:100%">
                  <el-option
                    v-for="item in rectificationEffectOptions"
                    :key="item.id"
                    :label="item.name"
                    :value="item.name"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
        </div>
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer" style="text-align:right">
      <el-button
        v-if="!criticalEducation && immediateRectification"
        type="primary"
        @click="handleSubmit('dialogData')"
        disabled
      >保存并返回</el-button>
      <el-button v-else type="primary" @click="handleSubmit('dialogData')">保存并返回</el-button>
      <el-button type="danger" @click="cancelDialog()" style="margin-left:20px">直接返回</el-button>
    </div>
    <el-dialog
    append-to-body
    title="监督责任人为领导，请指定审核人"
    :visible.sync="verifyPersonDialogVisible"
    width="30%">
    <el-form>
      <el-form-item
      label="审核人">
      <el-select
      v-model="verifyPersonEmpID"
      placeholder="请指定审核人"
      style="width:50%" 
      filterable 
      clearable>
      <el-option 
      v-for="item in verifyPersonOptions"
      :key="item.id"
      :label="`${item.name}`"
      :value="item.employeeID">
      </el-option>
      </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
    <el-button @click="cancelSetVerifyPerson">取 消</el-button>
    <el-button type="primary" @click="setVerifyPerson">确 定</el-button>
    </span>
    </el-dialog>
  </el-dialog>
</template>

<script>
import {
  GetCheckLevel,
  PostProblem,
  EditProblem
} from "../../../services/cq_problem_entr.js";
import { GetCompany } from "../../../services/gettreedata.js";
import { GetDictionary } from "../../../services/dictionary";
import { GetCurrentUser } from "../../../store/CurrentUser";
import {
  GetEmployees,
  //GetSubComUserEmployees
} from "../../../services/cq_problem_entr";
// import { GetEmployees } from '../../../services/employee';
import {CQGetverifyPerson} from '../../../services/cq_problem_acceptance'

export default {
  data() {
    return {
      dialogData: this.addData,
      checkTypeOptions: [],
      checkAttachLabel: `问题图片：\n最多上传四\n张图片，且\n每一张大小\n不超过12M!`,
      checkAttachFileList: [],
      checkVideourl: "",
      rectAttachLabel: `整改图片：\n最多上传四\n张图片，且\n每一张大小\n不超过12M!`,
      rectAttachFileList: [],
      rectVideourl: "",
      rectFileList: [],
      checkRankOptions: [],
      checkCompanyNameOptions: [],
      checkCompanyList: [],
      problemTypeOptions: [],
      problemRankOptions: [],
      problemSourceOptions: [],
      responsePersonOptions: [],
      supervisorOptions: [],
      immediateRectification: false,
      criticalEducation: false,
      scoreOptions: [],
      rectificationEffectOptions: [],
      workingAgeOptions: [],
      postNames: [],
      dialogVisible: this.show,
      rules: {
        responsePersonID: [
          { required: true, message: "请选择责任人", trigger: "change" }
        ],
        supervisorID: [
          { required: true, message: "请选择整改责任人", trigger: "change" }
        ]
      },
      popoverVisible: false,
      verifyPersonDialogVisible:false,
      verifyPersonOptions:[],
      position: '',
      leaderList:[
        '正处级',
        '副处级',
        '科级',
        '正科级',
        '副科级',
        '副总',
      ],
      verifyPersonEmpID: '',
      IsSetVerifyPerson: false,
      isImmediateRectification: true,
      types: []
    };
  },
  props: {
    show: {
      type: Boolean,
      default: false
    },
    addData: {
      type: Object,
      default: () => {}
    }
  },
  watch: {
    show() {
      this.dialogVisible = this.show;
    },
    addData() {
      this.checkCompanyList = this.addData.checkCompanyList;
      if (this.checkCompanyList) {
        let code =
          this.checkCompanyList[this.checkCompanyList.length - 1] || "01";
        this.handleGetEmployees(code);
        //this.handleGetSubComUserEmployees(code);
      }
      console.log(JSON.stringify(this.addData));
      if (this.addData.immediately === "是") {
        this.immediateRectification = true;
      }
      if (this.addData.education === "是") {
        this.criticalEducation = true;
      }
      this.dialogData = JSON.parse(
        JSON.stringify(this.addData).replace(
          /\b(execStaffName|execStaffID|cause|gist|hazardFactor|description|score)\b/g,
          function($0, $1) {
            return {
              execStaffName: "checkStaffName",
              execStaffID: "checkStaffID",
              cause: "problemSource",
              gist: "problemGist",
              hazardFactor: "problemType",
              description: "problemDescription",
              score: "lostScore"
            }[$1];
          }
        )
      );
      for (let i = 1; i < 5; i++) {
        if (this.addData[`checkAttach${i}`])
          this.checkAttachFileList.push({
            url: this.addData[`checkAttach${i}`]
          });
        if (this.addData[`rectAttach${i}`])
          this.rectAttachFileList.push({
            url: this.addData[`rectAttach${i}`]
          });
      }
      if (this.addData[`rectFile`])
        this.rectFileList.push({
          url: this.addData[`rectFile`],
          name: this.dealAttach(this.addData[`rectFile`])
        });
    },
    checkCompanyList() {
      if (this.$refs.refHandle) {
        this.$refs.refHandle.dropDownVisible = false;
      }
    }
  },
  computed: {
    currentUser() {
      return GetCurrentUser();
    }
  },
  mounted() {
    this.handleGetCheckLevel();
    this.handleGetCompany();
    this.getDictionaryCheckType();
    this.getDictionaryProblemType();
    this.getDictionaryProblemRank();
    this.getDictionaryProblemSource();
    this.getDictionaryWorkingAge();
    this.getDictionaryLostScore();
    this.getDictionaryRectEffect();
    this.getProblemType();
    this.getPostNames()
  },
  methods: {
    // 获取责任人
    handleGetEmployees(code) {
      GetEmployees(code)
        .then(res => {
          this.responsePersonOptions = res.data;
          this.supervisorOptions = res.data;
        })
        .catch(err => {
          this.$message.error(err.message);
          this.leaderLoading = false;
        });
    },
    // 获取整改责任人
    // handleGetSubComUserEmployees(code) {
    //   GetSubComUserEmployees(code)
    //     .then(res => {
    //       this.supervisorOptions = res.data;
    //     })
    //     .catch(err => {
    //       this.$message.error(err.message);
    //       this.leaderLoading = false;
    //     });
    // },
    handleGetCheckLevel() {
      GetCheckLevel()
        .then(res => {
          res.data.forEach(element => {
            this.checkRankOptions.push({ value: element, label: element });
          });
        })
        .catch(err => {
          this.$message.error(err.message);
        });
    },
    handleGetCompany() {
      GetCompany()
        .then(res => {
          this.checkCompanyNameOptions = res.data;
        })
        .catch(err => {
          this.$message.error(err.message);
        });
    },
    checkCompanyListChange() {
      if (this.checkCompanyList) {
        let code =
          this.checkCompanyList[this.checkCompanyList.length - 1] || "01";
        this.handleGetEmployees(code);
        //this.handleGetSubComUserEmployees(code);
      }
    },
    changeResponsibilityPerson(selectValue) {
      this.dialogData.employmentProperty = "";
      // this.dialogData.workingName = "";
      this.responsePersonOptions.forEach(element => {
        if (element.employeeID === selectValue) {
          this.dialogData.employmentProperty = element.empGroup;
          // this.dialogData.workingName = element.station;
        }
      });
    },
    getDictionaryCheckType() {
      GetDictionary({ name: "检查类型" })
        .then(res => {
          this.checkTypeOptions = res.data;
        })
        .catch(err => {
          this.$message.error(err.message);
        });
    },
    getDictionaryProblemType() {
      GetDictionary({ name: "问题类型" })
        .then(res => {
          this.problemTypeOptions = res.data;
        })
        .catch(err => {
          this.$message.error(err.message);
        });
    },
    getDictionaryProblemRank() {
      GetDictionary({ name: "隐患等级" })
        .then(res => {
          this.problemRankOptions = res.data;
        })
        .catch(err => {
          this.$message.error(err.message);
        });
    },
    getDictionaryProblemSource() {
      GetDictionary({ name: "问题原因" })
        .then(res => {
          this.problemSourceOptions = res.data;
        })
        .catch(err => {
          this.$message.error(err.message);
        });
    },
    getDictionaryWorkingAge() {
      GetDictionary({ name: "岗位工龄" })
        .then(res => {
          this.workingAgeOptions = res.data;
        })
        .catch(err => {
          this.$message.error(err.message);
        });
    },
    getPostNames(){
      GetDictionary({name: '岗位'}).then((res) => {
        this.postNames = res.data
      }).catch((err) => {
        this.$message.error(err.message)
      })
    },
    getDictionaryLostScore() {
      GetDictionary({ name: "扣分" })
        .then(res => {
          this.scoreOptions = res.data;
        })
        .catch(err => {
          this.$message.error(err.message);
        });
    },
    getDictionaryRectEffect() {
      GetDictionary({ name: "整改结果" })
        .then(res => {
          this.rectificationEffectOptions = res.data;
        })
        .catch(err => {
          this.$message.error(err.message);
        });
    },
    getProblemType() {
      GetDictionary({name:'检查类型'}).then((res) => {
        this.types = res.data
      }).catch((err) => {
        this.$message.error(err.message)
      })
    },
    changeImmediateRectification(value) {
      if (value) {
        this.criticalEducation = true;
        this.dialogData.verifyPersonName = this.dialogData.checkStaffName;
        this.dialogData.verifyPersonEmpID = this.dialogData.checkStaffID;
        this.dialogData.immediately = "是";
        this.dialogData.education = "是";
      } else {
        this.dialogData.immediately = "否";
        this.dialogData.lostScore = null;
        this.dialogData.penalty = null;
        this.dialogData.rectification = "";
        this.dialogData.education = "否";
        this.criticalEducation = false;
        this.rectAttachFileList = [];
        this.rectFileList = [];
        this.dialogData.rectVideo = "";
        this.dialogData.verifyPersonEmpID = null;
        this.dialogData.verifyPersonName = "";
        // this.dialogData.verifyDate = "";
        // this.dialogData.rectEffect = "";
      }
    },
    changeCriticalEducation(value) {
      if (value === true) {
        this.dialogData.education = "是";
      }
    },
    beforeAttachUpload(file) {
      let size = file.size / 1024 / 1024 / 12;
      if (size > 1) {
        this.$message.error("上传文件大小不能超过12MB！");
      }
    },
    handleRemoveCheckAttach(file, fileList) {
      this.checkAttachFileList = fileList;
    },
    handleSuccessCheckAttach(response, file, fileList) {
      if (response) {
        this.$notify.success({
          body: {
            style: { width: "300px", height: "500px" }
          },
          title: "提示",
          message: "文件上传成功"
        });
      }
      this.checkAttachFileList = fileList;
    },
    handleRemoveCheckVideo() {
      this.dialogData.checkVideo = "";
      this.checkVideourl = "";
    },
    handleSuccessCheckVideo(response, file) {
      if (response) {
        this.$notify.success({
          body: {
            style: { width: "300px", height: "500px" }
          },
          title: "提示",
          message: "问题视频上传成功"
        });
        this.dialogData.checkVideo = response.data;
        this.checkVideourl = URL.createObjectURL(file.raw);
      }
    },
    handleRemoveRectAttach(file, fileList) {
      this.rectAttachFileList = fileList;
    },
    handleSuccessRectAttach(response, file, fileList) {
      if (response) {
        this.$notify.success({
          body: {
            style: { width: "300px", height: "500px" }
          },
          title: "提示",
          message: "文件上传成功"
        });
      }
      this.rectAttachFileList = fileList;
    },
    handleRemoveRectVideo() {
      this.dialogData.rectVideo = "";
      this.rectVideourl = "";
    },
    handleSuccessRectVideo(response, file) {
      if (response) {
        this.$notify.success({
          body: {
            style: { width: "300px", height: "500px" }
          },
          title: "提示",
          message: "整改视频上传成功"
        });
        this.dialogData.rectVideo = response.data;
        this.rectVideourl = URL.createObjectURL(file.raw);
      }
    },
    handleRemoveRectFile(file, fileList) {
      this.rectFileList = fileList;
    },
    handleSuccessRectFile(response, file, fileList) {
      if (response) {
        this.$notify.success({
          body: {
            style: { width: "300px", height: "500px" }
          },
          title: "提示",
          message: "整改附件上传成功"
        });
        this.rectFileList = fileList;
      }
    },
    dealAttach(url) {
      if (url) return url.split("/").pop();
    },
    dealFileList(name, fileList) {
      if (name === "checkAttach" || name === "rectAttach") {
        for (let i = 1, len = fileList.length; i <= len; i++) {
          if (fileList[i - 1].response) {
            this.dialogData[`${name}${i}`] = fileList[i - 1].response.data;
          } else {
            this.dialogData[`${name}${i}`] = this.dealAttach(
              fileList[i - 1].url
            );
          }
        }
      } else {
        if (fileList[0].response) {
          this.dialogData[`${name}`] = fileList[0].response.data;
        } else {
          this.dialogData[`${name}`] = this.dealAttach(fileList[0].url);
        }
      }
    },
    handleSubmit(formName) {
      //console.log('indexOf',this.leaderList.indexOf(this.position))
      if(this.immediateRectification == false){
        this.dialogData.status = "未整改"
        if(this.leaderList.indexOf(this.position) !== -1) {
        if(this.IsSetVerifyPerson == false) {
          this.verifyPersonDialogVisible = true
          this.getCQverifyPerson()
        }
        else {
          this.$refs[formName].validate(valid => {
            if (valid) {
              this.submitFromData();
              } 
              else {
          return false;
        }
      });
        }
      }
      else {
        this.dialogData.verifyPersonName = this.dialogData.checkStaffName
        this.dialogData.verifyPersonEmpID = this.dialogData.checkStaffID
          this.$refs[formName].validate(valid => {
            if (valid) {
              this.submitFromData();
              } 
              else {
          return false;
        }
      });
      }
      }
      //console.log('dialogData',this.dialogData)
      else {
        this.dialogData.status = "已验证"
        this.$refs[formName].validate(valid => {
            if (valid) {
              this.submitFromData();
              } 
              else {
          return false;
        }
      });
      }
    },
    submitFromData() {
      console.log('dialogData',this.dialogData)
      if (this.checkAttachFileList.length > 0)
        this.dealFileList("checkAttach", this.checkAttachFileList);
      if (this.rectAttachFileList.length > 0)
        this.dealFileList("rectAttach", this.rectAttachFileList);
      if (this.rectFileList.length > 0)
        this.dealFileList("rectFile", this.rectFileList);
      if (this.dialogData.method === "addProblem") {
        this.dialogData.companyCode = this.checkCompanyList[
          this.checkCompanyList.length - 1
        ];
        this.dialogData.recordStatus = "暂存";
        if (this.dialogData.immediately == "否") {
          delete this.dialogData.verifyDate;
          delete this.dialogData.rectEffect;
        }
        //console.log(this.dialogData);
        PostProblem(this.dialogData)
          .then(res => {
            this.$message({
              message: res.message,
              type: "success"
            });
            this.seviceFunction();
            this.$emit("submitDialog", false);
          })
          .catch(err => {
            this.$message.error(err.message);
          });
      }
      if (this.dialogData.method === "editProblem") {
        this.dialogData.checkVideo = this.dealAttach(
          this.dialogData.checkVideo
        );
        this.dialogData.rectVideo = this.dealAttach(this.dialogData.rectVideo);
        this.dialogData.rectFile = this.dealAttach(this.dialogData.rectFile);
        // console.log('dialogData',this.dialogData);
        EditProblem(this.dialogData.checkRecordID, this.dialogData)
          .then(res => {
            this.$message({
              message: res.message,
              type: "success"
            });
            this.seviceFunction();
            this.$emit("submitDialog", false);
          })
          .catch(e => {
            this.$message.error(e.message);
          });
      }
      this.IsSetVerifyPerson = false
      this.verifyPersonEmpID = ''
      this.dialogData.verifyPersonName = ""
    },
    cancelDialog() {
      this.seviceFunction();
      this.$emit("cancelDialog", false);
    },
    seviceFunction() {
      this.dialogData = {};
      this.immediateRectification = false;
      this.criticalEducation = false;
      this.checkCompanyList = [];
      this.checkVideourl = "";
      this.rectVideourl = "";
      this.checkAttachFileList = [];
      this.rectAttachFileList = [];
      this.rectFileList = [];
    },
    //获取验证人
    getCQverifyPerson() {
      CQGetverifyPerson().then((res) => {
        this.verifyPersonOptions = res.data
        //console.log(res.data)
      })
    },
    cancelSetVerifyPerson() {
      this.dialogData.verifyPersonName = this.dialogData.checkStaffName;
      this.dialogData.verifyPersonEmpID = this.dialogData.checkStaffID;
      this.verifyPersonDialogVisible = false
    },
    setVerifyPerson() {
      this.dialogData.verifyPersonEmpID = this.verifyPersonEmpID
      //console.log('set',this.dialogData)
      this.verifyPersonDialogVisible = false
      this.IsSetVerifyPerson = true
    },
    supervisorChanged() {
      this.supervisorOptions.map(item =>{
        if(this.dialogData.supervisorID == item.employeeID) {
          this.position = item.position
          //console.log('position',this.position)
          }
          })
          if(this.leaderList.indexOf(this.position) == -1){
            this.isImmediateRectification = false
          }
          else{
            this.isImmediateRectification = true
          }
          
    }
  }
};
</script>